<?php
namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Closure;
use Illuminate\Support\Facades\Log;

class RequestLog
{

    public function handle(Request $request, Closure $next)
    {
        $traceId = md5(time() . mt_rand(1, 1000000));
        // 记录请求信息
        $requestMessage = [
            'url' => $request->path(),
            'method' => $request->method(),
            'ip' => $request->ips(),
            'headers' => $request->header(),
            'params' => $request->all()
        ];
        Log::channel('request')->info("请求信息：", $requestMessage);

        $respone = $next($request);
        $responeData = [
            'traceId' => $traceId,
            'respone' => json_decode($respone->getContent(), true) ?? ""
        ];
        Log::channel('request')->info("返回信息：", $responeData);
        return $respone;
    }
}